﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlReadLog : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindFile();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
        string filename = grv.Cells[2].Text;
        if (e.CommandName == "chon")
        {
           

                
                saveFile(this.Response,filename);
                
            
        }
        else if (e.CommandName == "xoa")
        {
            System.IO.FileInfo file = new System.IO.FileInfo(filename);
            if (file.Exists)
            {
                file.Delete();
                BindFile();
            }
        }
    }
    void saveFile(System.Web.HttpResponse page, string PathfileName)
    {
        string filename = PathfileName;
        if (filename != "")
        {

            System.IO.FileInfo file = new System.IO.FileInfo(filename);
            if (file.Exists)
            {
                page.Clear();
                page.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                page.AddHeader("Content-Length", file.Length.ToString());
                page.ContentType = "application/octet-stream";
                page.WriteFile(file.FullName);
                page.End();

            }
           
        }

    }
    void BindFile()
    {
        GetDirsFiles();
    }
    protected void btRequest_Click(object sender, EventArgs e)
    {
        BindFile();
    }
    public void GetDirsFiles()
    {
        //ArrayList list = new ArrayList();
        string[] file = Directory.GetFiles(Server.MapPath(BCDataCenter.Globals.LogUrl().ToString()));
        
        GridView1.DataSource = file;
        GridView1.DataBind();
    }
}
